const XLSX = require('xlsx');
const fs = require('fs'); //文件模块
const Path = require('path'); //系统路径模块

class iOSXlsClass {
  constructor(path) {
    this.xlsPath = path;
  }

  getHeaderRow(sheet) {
    const headers = []
    const range = XLSX.utils.decode_range(sheet['!ref'])
    let C
    const R = range.s.r
    /* start in the first row */
    for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
      const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
      /* find the cell in the first row */
      let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
      if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
      headers.push(hdr)
    }
    return headers
  }

  readData() {
    const workbook = XLSX.readFile(this.xlsPath, { type: 'array' })
    const firstSheetName = workbook.SheetNames[0]
    const worksheet = workbook.Sheets[firstSheetName]
    const results = XLSX.utils.sheet_to_json(worksheet)
    //console.log('result', results)
    this.generateI18nFiles(results)
  }

  generateI18nFiles(results) {
    const enData = {};
    const zhData = {};
    const hkData = {};

    const arData = {};
    const csData = {};
    const daData = {};
    const deData = {};
    const elData = {};
    const esData = {};
    const faData = {};
    const fiData = {};
    const frData = {};
    const gaData = {};
    const heData = {};
    const hrData = {};
    const huData = {};
    const itData = {};
    const jaData = {};
    const koData = {};
    const nbData = {};

    const plData = {};
    const ptData = {};
    const roData = {};
    const ruData = {};
    const skData = {};
    const svData = {};
    const thData = {};
    const trData = {};
    const ukData = {};
    const viData = {};

     

    results.forEach(element => {
      // console.log(element.key);

      enData[element.key] = element.en;
      zhData[element.key] = element.cn;
      hkData[element.key] = element.hk;

      frData[element.key] = element.fr;
      itData[element.key] = element.it;
      esData[element.key] = element.es;
      ptData[element.key] = element.pt;
      jaData[element.key] = element.ja;
      deData[element.key] = element.de;
      plData[element.key] = element.pl;
      huData[element.key] = element.hu;
      ruData[element.key] = element.ru;
      ukData[element.key] = element.uk;
      koData[element.key] = element.ko;
      viData[element.key] = element.vi;
      thData[element.key] = element.th;
      hrData[element.key] = element.hr;
      svData[element.key] = element.sv;
      nbData[element.key] = element.nb;
      daData[element.key] = element.da;
      fiData[element.key] = element.fi;
      csData[element.key] = element.cs;
      skData[element.key] = element.sk;
      gaData[element.key] = element.ga;
      arData[element.key] = element.ar;
      heData[element.key] = element.he;
      trData[element.key] = element.tr;
      roData[element.key] = element.ro;
      elData[element.key] = element.el;
      faData[element.key] = element.fa;


    });




    //la拉丁 ie爱尔兰语 无语言
    this.writeJson('./android_lang/values/strings.xml', this.formartXmlKeyValue(enData));
    this.writeJson('./android_lang/values-zh-rCN/strings.xml', this.formartXmlKeyValue(zhData));
    this.writeJson('./android_lang/values-zh-rTW/strings.xml', this.formartXmlKeyValue(hkData));

    this.writeJson('./android_lang/values-fr/strings.xml', this.formartXmlKeyValue(frData));
    this.writeJson('./android_lang/values-it/strings.xml', this.formartXmlKeyValue(itData));
    this.writeJson('./android_lang/values-es/strings.xml', this.formartXmlKeyValue(esData));
    this.writeJson('./android_lang/values-pt/strings.xml', this.formartXmlKeyValue(ptData));
    this.writeJson('./android_lang/values-ja/strings.xml', this.formartXmlKeyValue(jaData));
    this.writeJson('./android_lang/values-de/strings.xml', this.formartXmlKeyValue(deData));
    this.writeJson('./android_lang/values-pl/strings.xml', this.formartXmlKeyValue(plData));
    this.writeJson('./android_lang/values-hu/strings.xml', this.formartXmlKeyValue(huData));
    this.writeJson('./android_lang/values-ru/strings.xml', this.formartXmlKeyValue(ruData));
    this.writeJson('./android_lang/values-uk/strings.xml', this.formartXmlKeyValue(ukData));
    this.writeJson('./android_lang/values-ko/strings.xml', this.formartXmlKeyValue(koData));
    this.writeJson('./android_lang/values-vi/strings.xml', this.formartXmlKeyValue(viData));
    this.writeJson('./android_lang/values-th/strings.xml', this.formartXmlKeyValue(thData));
    this.writeJson('./android_lang/values-hr/strings.xml', this.formartXmlKeyValue(hrData));
    this.writeJson('./android_lang/values-sv/strings.xml', this.formartXmlKeyValue(svData));
    this.writeJson('./android_lang/values-nb/strings.xml', this.formartXmlKeyValue(nbData));
    this.writeJson('./android_lang/values-da/strings.xml', this.formartXmlKeyValue(daData));
    this.writeJson('./android_lang/values-fi/strings.xml', this.formartXmlKeyValue(fiData));
    this.writeJson('./android_lang/values-cs/strings.xml', this.formartXmlKeyValue(csData));
    this.writeJson('./android_lang/values-sk/strings.xml', this.formartXmlKeyValue(skData));
    this.writeJson('./android_lang/values-ga/strings.xml', this.formartXmlKeyValue(gaData));
    this.writeJson('./android_lang/values-ar/strings.xml', this.formartXmlKeyValue(arData));
    this.writeJson('./android_lang/values-he/strings.xml', this.formartXmlKeyValue(heData));
    this.writeJson('./android_lang/values-fa/strings.xml', this.formartXmlKeyValue(faData));
    this.writeJson('./android_lang/values-tr/strings.xml', this.formartXmlKeyValue(trData));
    this.writeJson('./android_lang/values-ro/strings.xml', this.formartXmlKeyValue(roData));
    this.writeJson('./android_lang/values-el/strings.xml', this.formartXmlKeyValue(elData));

  }
 
  formartXmlKeyValue(datas){
    if(datas == null) return "";
    var xml = '<?xml version="1.0" encoding="utf-8"?>\n';
    xml += "<resources>\n";

    var keys = Object.keys(datas);
    var values = Object.values(datas);
    for(var i = 0;i<keys.length;i++){
      var key = keys[i];
      var value = values[i];
      //console.log(key);
      //<string name="lan">tw</string>
      xml += "\t<string name=\""+key+"\">"+value+"</string>\n";
    }

    xml += "</resources>"
    return xml
  }

  writeJson(path, content) {
    const targetFile = Path.join(__dirname, path);

    fs.writeFile(targetFile, content, function (err) {
      if (err) {
        return console.error('error:' + err);
      }
      console.log('文件创建成功，地址：' + targetFile);
    });

  }
}

module.exports = iOSXlsClass